<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
	);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	
	if(!empty($_RQ['name'])){
		$params['name like']='%'.$_RQ['name'].'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(!empty($_RQ['account'])){
		$params['account like']='%'.$_RQ['account'].'%';
		$return['params']['account']=$_RQ['account'];
	}
	if(isset($_RQ['status'])){
		$params['status']=$_RQ['status'];
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$return['status']=array('0'=>'等待转账','1'=>'已转账','2'=>'驳回提现');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','mid','money','paytype','bank','bankhome','wechat','alipay','name','account','remark','createtime','dtime','status','type','mission','cause');
	$list=pdo_getslice('jy_jdpm_member_pledge_take',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
		foreach($list as &$item){
			$item['member']=get_member($item['mid']);
			$item['nickname']=$item['member']['nickname'];
			$item['avatar']=$item['member']['avatar'];
			$item['uaccount']=$item['member']['username'];
			$item['statuss']=$return['status'][$item['status']];
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['dtime']=$item['dtime']?date('Y/m/d H:i:s',$item['dtime']):'';
			$item['bankhome']=$item['bankhome']?$item['bankhome']:'空';
			
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'提现管理'),
		),
		'export'=>'export',
		'export_title'=>'导出提现数据',
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('type'=>'img_string','title'=>'用户账号','img'=>'avatar',
			'data'=>array(
				array('title'=>'账号：','name'=>'uaccount'),
				array('title'=>'昵称：','name'=>'nickname')
				)
			),
			array('name'=>'money','title'=>'提现金额','type'=>'string'),
			array('name'=>'mission','title'=>'手续费用','type'=>'string','options'=>'order_types'),
			array('type'=>'img_string','title'=>'收款账户',
			'data'=>array(
				array('title'=>'银行：','name'=>'bank'),
				array('title'=>'账号：','name'=>'account'),
				array('title'=>'姓名：','name'=>'name')
				)
			),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
			array('name'=>'createtime','title'=>'申请时间','type'=>'string'),
			array('type'=>'img_string','title'=>'状态',
			'data'=>array(
				array('title'=>'状态：','name'=>'statuss'),
				array('title'=>'时间：','name'=>'dtime'),
				array('title'=>'回复：','name'=>'cause')
				)
			),
		
		),
			'action'=>array(	
			array('op'=>'tixian','title'=>'处理','type'=>'modal','show'=>array('status'=>0)),
			//array('type'=>'null','show'=>array('status !='=>0)),
		),		
		),
		'search'=>array(
			array('type'=>'string','width'=>'13','title'=>'会员UID','name'=>'mid','help'=>''),
			array('type'=>'string','width'=>'13','title'=>'收款账户','name'=>'account','help'=>''),
			array('type'=>'string','width'=>'13','title'=>'姓名','name'=>'name','help'=>''),
			array('type'=>'select','width'=>'13','title'=>'状态','name'=>'status','options'=>'status2','select_title'=>'请选择提现状态'),
			array('type'=>'time_range','width'=>'16','title'=>'申请时间','name'=>'createtime','help'=>''),
		),	
	);
	
	$return['status2']=array(
		array('id'=>0,'title'=>'等待转账'),
		array('id'=>1,'title'=>'已转账'),
		array('id'=>2,'title'=>'驳回提现'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='tixian'){
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_jdpm_member_pledge_take',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$item['userinfo']='用户ID:'.$item['mid'].';真实姓名:'.$item['name'];
	$htmls=array(
		'type'=>'edit',
		'post'=>'set',
		'desc'=>'提现处理',
		'edit'=>array(
			array('type'=>'copy','title'=>'提现用户','name'=>'userinfo'),
			array('type'=>'copy','title'=>'提现金额','name'=>'money'),
			array('type'=>'radio','title'=>'处理结果','name'=>'status','options'=>'status'),
			array('type'=>'string','title'=>'回复','name'=>'cause'),
		)
	);
	$return['status']=array('1'=> '已转账','2'=> '驳回提现' ); 
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$info=pdo_get('jy_jdpm_member_pledge_take',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($info['status']){
		exi('请勿重复处理！','error');
	}
	$membersinfo=pdo_get('jy_jdpm_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$info['mid']));
	//不管提现驳回还是成功，都解冻！
	$freeze=$membersinfo['wallet_pledge_freeze']-$info['money']-$info['mission'];
	$wallet_pledge=$membersinfo['wallet_pledge'];
	if($post['status']==1){
		$wallet_pledge=$wallet_pledge-$info['money']-$info['mission'];
		$mlog=array(
			
                        'mid'          => $info['mid'],
                        'money'        => $info['money']+$info['mission'] ,
                        'channel'      => 1 ,
                        'create_time'  => time() ,
                        'update_time'  => time() ,
                        'auction_time' => 0 ,
                        'remarks'      => $post['cause'] ? $post['cause'] : '提现金额解冻' ,
						'role'         => '',
                        'type'         => 14 ,
						'uniacid'=>$_SESSION['uniacid'],
                       
		);
		pdo_insert('jy_jdpm_money_log',$mlog);
		$mlog=array(
			
                        'mid'          => $info['mid'],
                        'money'        => $info['money']+$info['mission'] ,
                        'channel'      => 2 ,
                        'create_time'  => time()+1 ,
                        'update_time'  => time()+1 ,
                        'auction_time' => 0 ,
                        'remarks'      => $post['cause'] ? $post['cause'] : '提现金额转出' ,
						'role'         => '',
                        'type'         => 13 ,
						'uniacid'=>$_SESSION['uniacid'],
                       
		);
		$userlog=array(
							'rtime' => time(),
							'status' => 2 ,
							'remarks' => $post['cause'] ? $post['cause'] : '提现成功金额转出'
		);
		pdo_insert('jy_jdpm_money_log',$mlog);
		pdo_update('jy_jdpm_freezelog',$userlog,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		
	}
	if($post['status']==2){
		$mlog=array(			
                        'mid'          => $info['mid'],
                        'money'        => $info['money']+$info['mission'] ,
                        'channel'      => 1 ,
                        'create_time'  => time() ,
                        'update_time'  => time() ,
                        'auction_time' => 0 ,
                        'remarks'      => $post['cause'] ? $post['cause'] : '提现金额解冻' ,
						'role'         => '',
                        'type'         => 14 ,
						'uniacid'=>$_SESSION['uniacid'],						
		);
		
		pdo_insert('jy_jdpm_money_log',$mlog);
		$userlog=array(
						'rtime' => time(),
						'status' => 1 ,
						'remarks'  => $post['cause'] ? $post['cause'] : '提现驳回金额解冻',
		);
		pdo_update('jy_jdpm_freezelog',$userlog,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		
	}
	
	pdo_update('jy_jdpm_member',array('wallet_pledge_freeze'=>$freeze,'wallet_pledge'=>$wallet_pledge),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$info['mid']));
	pdo_update('jy_jdpm_member_pledge_take',array('status'=>$post['status'],'dtime'=>TIMESTAMP,'cause'=>$post['cause']),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$info['mid'],'id'=>$id));
	if($post['status']==1){
		$msg = '您的提现申请已处理，请查询是否到账!';
		$this->model('notice')->set_websms($msg,0,$info['mid']);
	}
	if($post['status']==2){
		$msg = '您的提现申请已被驳回!原因：“'.$post['cause'].'”';
		$this->model('notice')->set_websms($msg,0,$info['mid']);
	}
	exi('处理成功','',manage_url('jy_jdpm/take/',true));
}
if($_RQ['op']=='export'){
	//导出体现数据
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
	);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['time between']=array($time_range['tstart'],$time_range['tend']);
	}
	if(!empty($_RQ['name'])){
		$params['name like']='%'.$_RQ['name'].'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(!empty($_RQ['account'])){
		$params['account like']='%'.$_RQ['account'].'%';
		$return['params']['account']=$_RQ['account'];
	}
	if(isset($_RQ['status'])){
		$params['status']=$_RQ['status'];
		$return['params']['status']=$_RQ['status'];
	}
	$list=pdo_getall('jy_jdpm_member_pledge_take',$params,array(),'','time asc');
	if($list){
		$status=array(
			'0'=>'等待退款',
			'1'=>'已退款',
			'2'=>'已驳回',
		);
		$type=array(
			'0'=>'招商银行',
			'1'=>'他行实时',
			
		);
		foreach($list as &$item){
			
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['dtime']=date('Y/m/d H:i:s',$item['dtime']);
			if($item['status']==2){
				$item['ddtime']=$item['dtime'];
				//$item['dtime']='';
			}
			if(!$item['dtime']){
				$item['dtime']='';
			}
			$item['status']=$status[$item['status']];
			$item['type']=$type[$item['type']];
			$item['url']=manage_url('jy_jdpm/membermoney/log',true).'/uid='.$item['mid'];
		}
	}
	$header=array(
		'mid'=>'付方账号',
		'money'=>'金额上限',
		'zhipiao'=>'支票权限',
		'shouquan'=>'授权使用人',
		'settype'=>'收方信息填写类型',
		'account'=>'收方账号',
		'name'=>'收方户名',
		'type'=>'汇路类型',
		'bank'=>'收方行名称',
		'bankhome'=>'收方行行号',
		'address'=>'收方行地址',
		'remark'=>'附言',
		'phone'=>'收款人手机号码',
		'url'=>'个人交易详情',
		'createtime'=>'申请时间',
		'dtime'=>'处理日期',
		'mission'=>'手续费',
	);
	cfc('tools')->export2excel($header,$list,'提现数据'.date('Y/m/d',time()));
}